package study.lxy.spring.springboot2.handler;

import com.fasterxml.jackson.core.JsonParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import springfox.documentation.spring.web.json.Json;
import springfox.documentation.spring.web.json.JsonSerializer;
import study.lxy.spring.springboot2.entity.MyResponseEntity;
@RestControllerAdvice
public class CommonResopnseHandler implements ResponseBodyAdvice<Object> {
    private Logger logger = LoggerFactory.getLogger(CommonResopnseHandler.class);
    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }
    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        logger.info("==========inside the message convert");
        String url = serverHttpRequest.getURI().getPath();
        if(url.indexOf("swagger") > -1 || url.indexOf("api-docs") > -1){
            return o;
        }
        if(o instanceof MyResponseEntity){
            return o;
        }
        return buildResponse(o);
    }

    private MyResponseEntity buildResponse(Object o){
        return new MyResponseEntity("0000","交易成功",o);
    }
}
